package com.teamfractal.fracdustry.client.screen.Machines;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.teamfractal.fracdustry.common.Fracdustry;
import com.teamfractal.fracdustry.common.container.Machines.FDElectricFurnaceContainer;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.FormattedCharSequence;
import net.minecraft.world.entity.player.Inventory;

import javax.annotation.Nonnull;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Tapio, mcjty
 */
public class FDElectricFurnaceScreen extends AbstractContainerScreen<FDElectricFurnaceContainer> {

    //todo:Should set to its own background
    private final ResourceLocation SCREEN =
            new ResourceLocation(Fracdustry.MODID, "textures/gui/electric_furnace/screen.png");

    public FDElectricFurnaceScreen(FDElectricFurnaceContainer container, Inventory inv, Component name) {
        super(container, inv, name);
    }

    @Override
    public void render(@Nonnull PoseStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        this.renderBackground(matrixStack);
        super.render(matrixStack, mouseX, mouseY, partialTicks);
        this.renderTooltip(matrixStack, mouseX, mouseY);
    }

    @Override
    protected void renderLabels(@Nonnull PoseStack matrixStack, int mouseX, int mouseY) {
        int relX = (this.width - 180) / 2;
        int relY = (this.height - 152) / 2;
        List<FormattedCharSequence> tooltip1 = new ArrayList<>();
        List<FormattedCharSequence> tooltip2 = new ArrayList<>();
        //Energy tooltip display
        if(mouseX > relX + 65 && mouseX < relX + 115 && mouseY > relY + 61 && mouseY < relY + 65){
            tooltip1.add(new TextComponent(new TranslatableComponent("gui.fracdustry.energy").getString()+
                    menu.getCurrentEnergy().get(0)+" / "+ menu.getMaxEnergy().get(0) +
                    " FE").getVisualOrderText());
            renderTooltip(matrixStack,tooltip1,mouseX - relX,mouseY - relY);}
        //Burn time tooltip display
        //Gotcha!
        double tm = menu.getProcessNow().get(0);
        int bt = Math.toIntExact(Math.round(tm / 20));
        if(mouseX > relX + 73 && mouseX < relX + 107 && mouseY > relY + 31 && mouseY < relY + 36){
            tooltip2.add(menu.getPowered()?
                    new TextComponent(MessageFormat.format(new TranslatableComponent("gui.fracdustry.process").getString()
                            , bt + new TranslatableComponent("units.fracdustry.sec")
                                    .getString())).getVisualOrderText()
                    : new TextComponent(new TranslatableComponent("gui.fracdustry.standby")
                    .getString()).getVisualOrderText()
            );
            renderTooltip(matrixStack,tooltip2,mouseX - relX,mouseY - relY);}
        }

    @Override
    protected void renderBg(@Nonnull PoseStack matrixStack, float partialTicks, int mouseX, int mouseY) {
        //Background
        RenderSystem.setShaderTexture(0, SCREEN);
        int relX = (this.width - 180) / 2;
        int relY = (this.height - 152) / 2;
        blit(matrixStack, relX, relY, 0, 0, 180, 152, 256, 256);

        //Energy Bar Display
        RenderSystem.setShaderTexture(0, SCREEN);
        double ce = menu.getCurrentEnergy().get(0);
        double me = menu.getMaxEnergy().get(0);
        int width = Math.toIntExact(Math.round((ce / me) * 49));
        blit(matrixStack, relX + 66, relY + 62, 180, 4, width, 3,256,256);
        //Process Bar Display
        RenderSystem.setShaderTexture(0, SCREEN);
        double pn = menu.getProcessNow().get(0);
        double pt = menu.getProcessTotal().get(0);
        long w = Math.round(((pt - pn) / pt) * 33);
        blit(matrixStack, relX + 74, relY + 32, 180, 0,
                menu.getPowered()?Math.toIntExact(w):0,4,
                256,256);
        //Powered Bar Display
        if (menu.getPowered()){
            RenderSystem.setShaderTexture(0, SCREEN);
            blit(matrixStack, relX + 84, relY + 47, 180, 7, 12, 1,256,256);
            blit(matrixStack, relX + 84, relY + 52, 180, 7, 12, 1,256,256);
        }
        //Title
        this.font.draw(matrixStack,
                new TranslatableComponent("gui.fracdustry.title.electric_furnace").getString()
                , relX + 4, relY + 4, -1);
    }
}
